package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.domain.ReportRuleTask;
import com.ruoyi.system.mapper.ReportRuleTaskMapper;
import com.ruoyi.system.service.IReportRuleTaskService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报告任务规则Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-23
 */
@Service
public class ReportRuleTaskServiceImpl implements IReportRuleTaskService {
    @Autowired
    private ReportRuleTaskMapper reportRuleTaskMapper;
    @Autowired
    private ISysJobService sysJobServices;

    /**
     * 查询报告任务规则
     *
     * @param id 报告任务规则ID
     * @return 报告任务规则
     */
    @Override
    public ReportRuleTask selectReportRuleTaskById(Long id) {
        ReportRuleTask task= reportRuleTaskMapper.selectReportRuleTaskById(id);
        SysJob job = sysJobServices.selectJobById(task.getTaskId());
        task.setJob(job);
        return task;
    }

    /**
     * 查询报告任务规则列表
     *
     * @param reportRuleTask 报告任务规则
     * @return 报告任务规则
     */
    @Override
    public List<ReportRuleTask> selectReportRuleTaskList(ReportRuleTask reportRuleTask) {
        List<ReportRuleTask> list = reportRuleTaskMapper.selectReportRuleTaskList(reportRuleTask);
        list.forEach(reportRuleTask1 -> {
            SysJob job = sysJobServices.selectJobById(reportRuleTask1.getTaskId());
            reportRuleTask1.setJob(job);
        });
        return list;
    }

    /**
     * 新增报告任务规则
     *
     * @param reportRuleTask 报告任务规则
     * @return 结果
     */
    @Override
    public int insertReportRuleTask(ReportRuleTask reportRuleTask) {
        try {
            sysJobServices.insertJob(reportRuleTask.getJob());
            reportRuleTask.setTaskId(reportRuleTask.getJob().getJobId());
            return reportRuleTaskMapper.insertReportRuleTask(reportRuleTask);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return 0;
        } catch (TaskException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改报告任务规则
     *
     * @param reportRuleTask 报告任务规则
     * @return 结果
     */
    @Override
    public int updateReportRuleTask(ReportRuleTask reportRuleTask) {
        try {
            sysJobServices.updateJob(reportRuleTask.getJob());
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (TaskException e) {
            e.printStackTrace();
        }
        return reportRuleTaskMapper.updateReportRuleTask(reportRuleTask);
    }

    /**
     * 批量删除报告任务规则
     *
     * @param ids 需要删除的报告任务规则ID
     * @return 结果
     */
    @Override
    public int deleteReportRuleTaskByIds(Long[] ids) {
        List<ReportRuleTask> list = reportRuleTaskMapper.selectReportRuleTaskByIds(ids);
        List<Long> jobIds = new ArrayList<>();
        list.forEach(reportRuleTask -> {
            jobIds.add(reportRuleTask.getTaskId());
        });
        if(jobIds.size()>0){
            try {
                sysJobServices.deleteJobByIds(jobIds.toArray(new Long[jobIds.size()]));
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
        return reportRuleTaskMapper.deleteReportRuleTaskByIds(ids);
    }

    /**
     * 删除报告任务规则信息
     *
     * @param id 报告任务规则ID
     * @return 结果
     */
    @Override
    public int deleteReportRuleTaskById(Long id) {
        return reportRuleTaskMapper.deleteReportRuleTaskById(id);
    }
}
